package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.millioncall.openplatform.util.NumberUtils;
import org.millioncall.yueyoga.admin.dao.CashDAO;
import org.millioncall.yueyoga.admin.dao.OrderTypeDAO;
import org.millioncall.yueyoga.admin.dao.PayTypeDAO;
import org.millioncall.yueyoga.admin.model.Cash;
import org.millioncall.yueyoga.admin.model.CashDetail;
import org.millioncall.yueyoga.admin.model.CashModel;
import org.millioncall.yueyoga.admin.model.CashReport;
import org.millioncall.yueyoga.admin.service.CashService;

public class CashServiceImpl implements CashService {
	
	private CashDAO cashDAO;
	private PayTypeDAO payTypeDAO;
	private OrderTypeDAO orderTypeDAO;

	public CashDAO getCashDAO() {
		return cashDAO;
	}

	public void setCashDAO(CashDAO cashDAO) {
		this.cashDAO = cashDAO;
	}

	public PayTypeDAO getPayTypeDAO() {
		return payTypeDAO;
	}

	public void setPayTypeDAO(PayTypeDAO payTypeDAO) {
		this.payTypeDAO = payTypeDAO;
	}

	public OrderTypeDAO getOrderTypeDAO() {
		return orderTypeDAO;
	}

	public void setOrderTypeDAO(OrderTypeDAO orderTypeDAO) {
		this.orderTypeDAO = orderTypeDAO;
	}

	@Override
	public CashModel findCash(Map<String, Object> map) {
		CashModel cm = new CashModel();
		Map<String, CashReport> cashMap = new HashMap<String, CashReport>();
		List<CashReport> list = new ArrayList<CashReport>();
		List<Cash> oldList = this.cashDAO.findCashDetail(map);
		for(int i=0;i<oldList.size();i++){
			Cash c = oldList.get(i);
			if (cashMap.containsKey(c.getType())) {
				CashReport crt = cashMap.get(c.getType());
				crt.setOrganizationId(c.getOrganizationId());
				crt.setOrganizationName(c.getOrganizationName());
				if("现金".equals(c.getPayType())){
					crt.setXjMoney(c.getMoney());
				}else if("刷卡".equals(c.getPayType())){
					crt.setSkMoney(c.getMoney());
				}else if("支票".equals(c.getPayType())){
					crt.setZpMoney(c.getMoney());
				}else if("转账".equals(c.getPayType())){
					crt.setZzMoney(c.getMoney());
				}
			}else{
				CashReport cr = new CashReport();
				cr.setType(c.getType());
				cr.setOrganizationId(c.getOrganizationId());
				cr.setOrganizationName(c.getOrganizationName());
				if("现金".equals(c.getPayType())){
					cr.setXjMoney(c.getMoney());
				}else if("刷卡".equals(c.getPayType())){
					cr.setSkMoney(c.getMoney());
				}else if("支票".equals(c.getPayType())){
					cr.setZpMoney(c.getMoney());
				}else if("转账".equals(c.getPayType())){
					cr.setZzMoney(c.getMoney());
				}
				cashMap.put(cr.getType(), cr);			}
		}
		
		Iterator<Entry<String, CashReport>> it = cashMap.entrySet().iterator();
		Double zj = 0.00, xjzj = 0.00, skzj = 0.00, zpzj = 0.00, zzzj = 0.00;
		while(it.hasNext()){
			CashReport cr = it.next().getValue();
			Double money = NumberUtils.add(NumberUtils.add(NumberUtils.add(NumberUtils.add(cr.getMoney(), cr.getZzMoney()), cr.getZpMoney()), cr.getSkMoney()), cr.getXjMoney());
			cr.setMoney(money);
			zj = NumberUtils.add(zj, money);
			xjzj = NumberUtils.add(xjzj, cr.getXjMoney());
			skzj = NumberUtils.add(skzj, cr.getSkMoney());
			zpzj = NumberUtils.add(zpzj, cr.getZpMoney());
			zzzj = NumberUtils.add(zzzj, cr.getZzMoney());
			list.add(cr);
		}
		
		cm.setCashReportList(list);
		cm.setZj(zj);
		cm.setXjzj(xjzj);
		cm.setSkzj(skzj);
		cm.setZpzj(zpzj);
		cm.setZzzj(zzzj);
		return cm;
	}
	
	public List<Cash> findType(String type, List<Cash> list) {
		List<Cash> newList = new ArrayList<Cash>();
		for (Cash cash : list) {
			if (type.equals(cash.getType()))
				newList.add(cash);
		}
		return newList;
	}

	@Override
	public List<Cash> findCashDetail(Map<String, Object> map) {
		return this.cashDAO.findCashDetail(map);
	}

	@Override
	public List<CashDetail> findCashDetailByType(Map<String, Object> map) {
		List<CashDetail> list = this.cashDAO.findCashDetailByType(map);
		
		for (CashDetail cd : list) {
			if (cd.getPayType() == 1)
				cd.setXj(cd.getMoney());
			else if (cd.getPayType() == 2)
				cd.setSk(cd.getMoney());
			else if (cd.getPayType() == 3)
				cd.setZz(cd.getMoney());
			else if (cd.getPayType() == 4)
				cd.setZp(cd.getMoney());
		}
		return list;
	}
}
